<?php
namespace app\api\controller;

use app\admin\model\Car as CarModel;
use app\admin\model\CarBrand;
use app\common\controller\Api;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class Driver extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 申请成为司机
     *
     * @return void
     */
    public function apply()
    {
        $uid = $this->auth->id;
        $username = $this->request->post('username');
        $id_card = $this->request->post('id_card');
        $contact_number = $this->request->post('contact_number');
        $standby_telephone = $this->request->post('standby_telephone');
        $front_id_card_img = $this->request->post('front_id_card_img');
        $backend_id_card_img = $this->request->post('backend_id_card_img');
        $license_img = $this->request->post('license_img');
        if (!$username || !$id_card || !$contact_number || !$standby_telephone || !$front_id_card_img || !$backend_id_card_img || !$license_img) {
            $this->error('参数错误');
        }
        $userInfo = $this->auth->getUserInfo();
        if ($userInfo['cate'] == 1) {
            $this->error('您已经是车主，不能重复申请');
        }
        if ($userInfo['cate'] == 2 || $userInfo['cate'] == 3 || $userInfo['cate'] == 4) {
            $this->error('您存在其他身份，无法申请');
        }
        $data = [
            'uid' => $uid,
            'username' => $username,
            'id_card' => $id_card,
            'contact_number' => $contact_number,
            'standby_telephone' => $standby_telephone,
            'front_id_card_img' => $front_id_card_img,
            'backend_id_card_img' => $backend_id_card_img,
            'license_img' => $license_img,
        ];
        $res = (new \app\admin\model\Driver())->save($data);
        if ($res) {
            $this->success('申请成功, 请耐心等待审核');
        } else {
            $this->error('申请失败');
        }
    }

    /**
     * 任务列表
     *
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function taskList()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $model = (new \app\admin\model\Project());
        $projectBindModel = (new \app\admin\model\ProjectBind());
        $list = $model->order('id desc')->page($page, $limit)->select();
        if (!$list) {
            $this->error('暂无数据');
        }
        foreach ($list as $key => $value) {
            $projectBind = $projectBindModel->with('owner')->where('project_id', $value['id'])->find();
            $list[$key]['bind'] = $projectBind;
        }
        $count = $model->count();
        $data = [
            'list' => $list,
            'count' => $count
        ];
        $this->success('获取成功', $data);
    }

    /**
     * 车辆列表
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function carList()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $carModel = new CarModel();
        $list = $carModel->page($page, $limit)->select();
        foreach ($list as $key => $value) {
            $carBrand = (new CarBrand())->where('id', $value['brand_id'])->find();
            $list[$key]['brand'] = $carBrand ? $carBrand->toArray() : [];
            $owner = new \app\admin\model\Owner();
            $owner = $owner->where('uid', $value['uid'])->where('status', 2)->find();
            $list[$key]['owner'] = $owner ? $owner->toArray() : [];
        }
        $count = $carModel->count();
        $data = [
            'count' => $count,
            'list' => $list
        ];
        $this->success('', $data);
    }

    /**
     * 车辆绑定司机
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function carBind()
    {
        $car_id = $this->request->post('car_id');
        $uid = $this->auth->id;
        $model = new \app\admin\model\Driver();
        $driver = $model->where('uid', $uid)->where('status', 1)->find();
        if (!$driver) {
            $this->error('您还未申请成为司机');
        }
        $data = [
            'car_id' => $car_id,
            'driver_id' => $driver->id
        ];
        $res = (new \app\admin\model\CarBindLog())->save($data);
        if ($res) {
            $this->success('提交成功，请耐心等待审核');
        } else {
            $this->error('提交失败');
        }
    }

    /**
     * 绑定司机
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function bindDriver()
    {
        $car_id = $this->request->post('car_id');
        $project_id = $this->request->post('project_id');
        $uid = $this->auth->id;
        $model = new \app\admin\model\Driver();
        $driver = $model->where('uid', $uid)->where('status', 1)->find();
        if (!$driver) {
            $this->error('您还未申请成为司机');
        }
        $data = [
            'car_id' => $car_id,
            'driver_id' => $driver->id,
            'project_id' => $project_id
        ];
        $res = (new \app\admin\model\ProjectBindDriver())->save($data);
        if ($res) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }
    }
}